<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Action.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/actions/Response.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/Service.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Person.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Error.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/const/Pattern.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/utils/Str.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/model/Service/ServiceFactory.php';

final class PersonUpdateAction extends Action {
	protected function accessIdentities() {
		return null;
	}

	protected function execute(Response $response) {
		if(isset($_SESSION['user'])) {
			$person = unserialize($_SESSION['user']);
			if(isset($_POST['surname']) && Str::isMatch(Pattern::WORD, $_POST['surname'])) {
				$person->setSurname($_POST['surname']);
			}
			if(isset($_POST['name']) && Str::isMatch(Pattern::WORD, $_POST['name'])) {
				$person->setName($_POST['name']);
			}
			if(isset($_POST['patronymic']) && Str::isMatch(Pattern::WORD, $_POST['patronymic'])) {
				$person->setPatronymic($_POST['patronymic']);
			}
			$serviceFactory = new ServiceFactory(); //Создаем фабрику сервисов
			$service = $serviceFactory->getService('PersonService');//  Получаем сервис
			$person = $service->save($person);// Выполняем действие
			$_SESSION['user'] = serialize($person);
			$response->setActionPath('/person/editPrivate.html');
		} else {
			$response->setActionPath('/login.html');
		}
	}
}
?>